﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using Runda.EShop.Common.Utility;
using System.Data.SqlClient;
using Runda.EShop.Common.Entity;

namespace Runda.EShop.Dal
{
    /// <summary>
    /// DAL layer class dbo.Relation_Customer_ProductCate.
    /// </summary>
    public partial class RelationCustomerProductCateDal 
    {
        public void BulkInsert(List<RelationCustomerProductCateEntity> list)
        {
            SqlBulkCopy bulkCopy;

            if (base.Tran != null)
            {
                SqlConnection cnx = Tran.Connection as SqlConnection;
                SqlTransaction transaction = Tran as SqlTransaction;
                bulkCopy = new SqlBulkCopy(cnx, SqlBulkCopyOptions.CheckConstraints, transaction);
            }
            else
                bulkCopy = new SqlBulkCopy(CurrentDatabase.ConnectionString, SqlBulkCopyOptions.CheckConstraints);

            bulkCopy.BulkCopyTimeout = 360;
            bulkCopy.DestinationTableName = "Relation_Customer_ProductCate";

            DataTable dataTable = new DataTable();
            DataColumn col0 = dataTable.Columns.Add("ID", typeof(System.Guid));
            col0.AllowDBNull = false;
            DataColumn col1 = dataTable.Columns.Add("CustomerID", typeof(System.Guid));
            col1.AllowDBNull = true;
            DataColumn col2 = dataTable.Columns.Add("CateCode", typeof(System.String));
            col2.AllowDBNull = true;
            DataColumn col3 = dataTable.Columns.Add("Deleted", typeof(System.Boolean));
            col3.AllowDBNull = true;
            DataColumn col4 = dataTable.Columns.Add("CateType", typeof(System.Int16));
            col4.AllowDBNull = false;

            bulkCopy.ColumnMappings.Add("ID", "ID");
            bulkCopy.ColumnMappings.Add("CustomerID", "CustomerID");
            bulkCopy.ColumnMappings.Add("CateCode", "CateCode");
            bulkCopy.ColumnMappings.Add("Deleted", "Deleted");
            bulkCopy.ColumnMappings.Add("CateType", "CateType");

            foreach (RelationCustomerProductCateEntity entity in list)
            {
                DataRow row = dataTable.NewRow();

                row["ID"] = entity.ID;
                row["CustomerID"] = entity.CustomerID != Guid.Empty ? (object)entity.CustomerID : System.DBNull.Value;
                row["CateCode"] = entity.CateCode != null ? (object)entity.CateCode : System.DBNull.Value;
                row["Deleted"] = (object)entity.Deleted;
                row["CateType"] = (object)entity.CateType;

                dataTable.Rows.Add(row);
            }

            bulkCopy.WriteToServer(dataTable);       
        }

        public void DeleteByCustomer(Guid CustomerID)
        {
            string sql = "delete from Relation_Customer_ProductCate where CustomerID = @CustomerID";

            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@CustomerID", DbType.Guid, CustomerID);

            if(base.Tran != null)
            CurrentDatabase.ExecuteNonQuery(dbcommand,Tran);
            else
                CurrentDatabase.ExecuteNonQuery(dbcommand);
        }

        public void DeleteByCustomer(Guid CustomerID,int CateType)
        {
            string sql = "delete from Relation_Customer_ProductCate where CustomerID = @CustomerID and CateType =@CateType";

            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@CustomerID", DbType.Guid, CustomerID);
            CurrentDatabase.AddInParameter(dbcommand, "@CateType", DbType.Int16, CateType);

            if(base.Tran != null)
            CurrentDatabase.ExecuteNonQuery(dbcommand,Tran);
            else
                CurrentDatabase.ExecuteNonQuery(dbcommand);
        }

    }
}
